﻿using Org.BouncyCastle.Crypto.Digests;
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Numerics;
using Nethereum.Hex.HexConvertors.Extensions;
using Nethereum.Hex.HexTypes;
using Nethereum.RLP;
using Nethereum.Signer;
using Nethereum.Util;

namespace BlockchainCrypto
{
    public class Transaction
    {
        public static void Main()
        {
            string to = "lat1klsjfxjwq2dvdd567wz7qwrjjluvzvsrf2t9k3";
            BigInteger amount = new BigInteger(1e18);
            var nonce = new BigInteger(1);
            var gasPrice = new BigInteger(1000000000);
            var gasLimit = new BigInteger(21000);
            var data = "";
            var chainId = new BigInteger(210309);
            var tx = new LegacyTransactionChainId(to, amount, nonce, gasPrice, gasLimit, data, chainId);
            var privateKey = "";
            var platonKey = new EthECKey(privateKey.HexToByteArray(), true);
            tx.Sign(platonKey);

            //string to = "lat1klsjfxjwq2dvdd567wz7qwrjjluvzvsrf2t9k3";
            //Bech32Engine.Decode(to, out string hrp, out byte[] to_bytes);
            //var transaction = new
            //{
            //    to = to_bytes,
            //    gasPrice = 1000000000,
            //    gas = 21000,
            //    nonce = 1,
            //    data = new byte[0],
            //    v = 210309,
            //    r = 0,
            //    s = 0,
            //    value = 1000000000000000000
            //};
            //var encoded_tx = RLP.Encode(transaction);

            ////keccak
            //var hashAlgorithm = new KeccakDigest(256);
            //hashAlgorithm.BlockUpdate(encoded_tx, 0, encoded_tx.Length);
            //byte[] result = new byte[32]; // 256 / 8 = 32
            //hashAlgorithm.DoFinal(result, 0);











            //var privateKey = "b5b1870957d373ef0eeffecc6e4812c0fd08f554b37b233526acc331bf1544f7";
            //var sendersAddress = "12890d2cce102216644c59daE5baed380d84830c";
            //var publicKey = "87977ddf1e8e4c3f0a4619601fc08ac5c1dcf78ee64e826a63818394754cef52457a10a599cb88afb7c5a6473b7534b8b150d38d48a11c9b515dd01434cceb08";

            ////data use for other tools for comparison
            ////Debug.WriteLine(new HexBigInteger(10000).HexValue);
            ////Debug.WriteLine(new HexBigInteger(324).HexValue);
            ////Debug.WriteLine(new HexBigInteger(10000000000000).HexValue);
            ////Debug.WriteLine(new HexBigInteger(21000).HexValue);

            ////Create a transaction from scratch
            //var tx = new Transaction("0x13f022d72158410433cbd66f5dd8bf6d2d129924", 10000, 324, 10000000000000, 21000);
            //tx.Sign(new ECKey(privateKey.HexToByteArray(), true));

            //var encoded = tx.GetRLPEncoded();
            //var rlp =
            //    "f8698201448609184e72a0008252089413f022d72158410433cbd66f5dd8bf6d2d129924822710801ca0b1874eb8dab80e9072e57b746f8f0f281890568fd655488b0a1f5556a117775ea06ea87e03a9131cae14b5420cbfeb984bb2641d76fb32327d87cf0c9c0ee8f234";

            //Assert.Equal(rlp, encoded.ToHex());
            ////data used for other tools for comparison
            //Debug.WriteLine(encoded.ToHex());

            //Assert.Equal(EthECKey.GetPublicAddress(privateKey), tx.Key.GetPublicAddress());

            //var tx3 = new Transaction(rlp.HexToByteArray());
            //Assert.Equal(tx.Data, tx3.Data ?? new byte[] { });

            //Debug.WriteLine(tx.ToJsonHex());

            //var tx2 = new Transaction(tx.GetRLPEncoded());
            //Assert.Equal(EthECKey.GetPublicAddress(privateKey), tx2.Key.GetPublicAddress());

            //Assert.Equal(tx.GasLimit.ToHex(), tx3.GasLimit.ToHex());
            //Assert.Equal(tx.Nonce.ToHex(), tx3.Nonce.ToHex());
            //Assert.Equal(tx.GasPrice.ToHex(), tx3.GasPrice.ToHex());
            //Assert.Equal(tx.Value.ToHex(), tx3.Value.ToHex());
            //Assert.Equal(tx.RawHash.ToHex(), tx3.RawHash.ToHex());
            //Assert.Equal(tx3.Key.GetPublicAddress(), tx.Key.GetPublicAddress());
            //Assert.Equal(tx2.RawHash.ToHex(), tx3.RawHash.ToHex());
            //Assert.Equal(tx2.Key.GetPublicAddress(), tx.Key.GetPublicAddress());








        }
    }
}
